<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2017 河源市卓锐科技有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------
// | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------

namespace app\member\home;

use app\index\controller\User;
use app\member\model\Member as MemberModel;
use think\Db;
/**             
 * 会员中心控制器
 * @package app\admin\controller
 */
class Member extends User
{
    protected function _initialize(){
        parent::_initialize();

    }
    public function index(){
        return $this->fetch();
    }
    //安全设置
    public function save(){
        $data=Db::name("member")->where('id',$this->mid)->find();
        $this->assign("data",$data);
        return $this->fetch();
    }
    /**
     * 修改密码
     * @return void
     * @author yiyouguisu<741459065@qq.com> time|20161111
     */
    public function changepassword() {
        if(request()->isPost()){
            $ret=$_POST;
            $mobile=input('post.mobile');
            $telverify=input('post.telverify');
            $old_password=input('post.old_password');
            $new_password=input('post.new_password');
            $imagecode=input('post.verify');
            $checkverify=input('post.checkverify');

            $user=Db::name('Member')->where('id',$this->mid)->find();
            if(empty($user)) return json(['code'=>-200,'msg'=>'用户不存在']);

            $verify=getverify($mobile,0);
            if(strtolower($telverify)!=strtolower($verify)){
                return json(['code'=>-200,'msg'=>'请输入您接收到的正确验证码']);
            }
            if ($checkverify&&!captcha_check($imagecode, '', config('captcha'))) {
                return json(['code'=>-200,'msg'=>'验证码错误']);
            }

            if(MemberModel::encryption($old_password, $user['verify'])!=$user['password']){
                return json(['code'=>-200,'msg'=>'原密码错误']);
            }

            $verify = genRandomString(6);
            $data['verify'] = $verify;
            $data['password'] = MemberModel::encryption($new_password, $verify);
            $id=Db::name('Member')->where('id',$this->mid)->update($data);
            if($id){
                return json(['code'=>200,'msg'=>'修改成功']);
            }else{
                return json(['code'=>-200,'msg'=>'修改失败']);
            }
            
        }else{
            $action=$this->request->action();
            $controller=$this->request->controller();
            $map['isdel']=0;
            $map['scenecode']=$controller.'_'.$action;
            $map['status']=1;
            $validation_scene=Db::name('validation_scene')->where($map)->field('id,status')->find();
            if($validation_scene&&$validation_scene['status']==1){
                $check_verify=1;
            }else{
                $check_verify=0;
            }
            $this->assign('check_verify',$check_verify);
            return $this->fetch();
        }
    }
    /**
     * 会员修改手机号
     * @return void
     * @author yiyouguisu<741459065@qq.com> time|20161111
     */
    public function changephone(){
        if(request()->isPost()){
            $ret=$_POST;
            $old_mobile=input('post.old_mobile');
            $new_mobile=input('post.new_mobile');
            $new_telverify=input('post.new_telverify');
            $old_telverify=input('post.old_telverify');
            $imagecode=input('post.verify');
            $checkverify=input('post.checkverify');

            
            $user=Db::name('Member')->where('id',$this->mid)->find();
            if(empty($user)) return json(['code'=>-200,'msg'=>'用户不存在']);

            $old_verify=getverify($old_mobile,0);
            if(strtolower($old_telverify)!=strtolower($old_verify)){
                return json(['code'=>-200,'msg'=>'请输入您接收到的正确验证码']);
            }
            $new_verify=getverify($new_mobile,0);
            if(strtolower($new_telverify)!=strtolower($new_verify)){
                return json(['code'=>-200,'msg'=>'请输入您接收到的正确验证码']);
            }
            if ($checkverify&&!captcha_check($imagecode, '', config('captcha'))) {
                return json(['code'=>-200,'msg'=>'验证码错误']);
            }

            if(!preg_match("/^1[0-9]{10}$/",$new_mobile)) return json(['code'=>-200,'msg'=>'新手机号码格式错误']);

            $check_result=Db::name('Member')->where(['id'=>['neq',$this->mid],'mobile'=>$new_mobile])->count();
            if($check_result) return json(['code'=>-200,'msg'=>'手机号已经被占用']);
            
            $id=Db::name('Member')->where("id",$this->mid)->update(['mobile'=>$new_mobile,'mobile_status'=>1]);
            if($id){
                session("mobile",$new_mobile);
                return json(['code'=>200,'msg'=>'修改成功']);
            }else{
                return json(['code'=>-200,'msg'=>'修改失败']);
            }
            
        }else{
            $action=$this->request->action();
            $controller=$this->request->controller();
            $map['isdel']=0;
            $map['scenecode']=$controller.'_'.$action;
            $map['status']=1;
            $validation_scene=Db::name('validation_scene')->where($map)->field('id,status')->find();
            if($validation_scene&&$validation_scene['status']==1){
                $check_verify=1;
            }else{
                $check_verify=0;
            }
            $this->assign('check_verify',$check_verify);
            return $this->fetch();
        }
    }
    /**
     * 会员修改邮箱
     * @return void
     * @author yiyouguisu<741459065@qq.com> time|20161111
     */
    public function changeemail(){
        if(request()->isPost()){
            $ret=$_POST;
            $mobile=input('post.mobile');
            $new_email=input('post.new_email');
            $telverify=input('post.telverify');
            $imagecode=input('post.verify');
            $checkverify=input('post.checkverify');

            
            $user=Db::name('Member')->where('id',$this->mid)->find();
            if(empty($user)) return json(['code'=>-200,'msg'=>'用户不存在']);

            $verify=getverify($mobile,0);
            if(strtolower($telverify)!=strtolower($verify)){
                return json(['code'=>-200,'msg'=>'请输入您接收到的正确验证码']);
            }
            if ($checkverify&&!captcha_check($imagecode, '', config('captcha'))) {
                return json(['code'=>-200,'msg'=>'验证码错误']);
            }

            if(!preg_match("/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/",$new_email)) return json(['code'=>-200,'msg'=>'新手机号码格式错误']);

            $check_result=Db::name('Member')->where(['id'=>['neq',$this->mid],'email'=>$new_email])->count();
            if($check_result) return json(['code'=>-200,'msg'=>'邮箱账号已经被占用']);
            
            $id=Db::name('Member')->where("id",$this->mid)->update(['email'=>$new_email,'email_status'=>1]);
            if($id){
                return json(['code'=>200,'msg'=>'修改成功']);
            }else{
                return json(['code'=>-200,'msg'=>'修改失败']);
            }
            
        }else{
            $action=$this->request->action();
            $controller=$this->request->controller();
            $map['isdel']=0;
            $map['scenecode']=$controller.'_'.$action;
            $map['status']=1;
            $validation_scene=Db::name('validation_scene')->where($map)->field('id,status')->find();
            if($validation_scene&&$validation_scene['status']==1){
                $check_verify=1;
            }else{
                $check_verify=0;
            }
            $this->assign('check_verify',$check_verify);
            return $this->fetch();
        }
    }

    /**
    *子账户
    */
    public function subbill(){
        $uid = session('uid');

        $subbillname = input('subbillname');
        if(!empty($subbillname)){
            $where['subbillname'] = array('like',"%$subbillname%");
        }

        $name = input('name');
        if(!empty($name)){
            $where['name'] = array('like', "%$name%");
        }
        $where['parentid'] = $uid;

        $data_list = db('member a')
                    ->join('member_role b', 'a.roleid=b.roleid', 'left')
                    ->where($where)
                    ->field('a.*,b.rolename')
                    ->order('update_time DESC')
                    ->paginate(20,false,['query' => request()->param(false)]);
        $all_page = $data_list->lastPage();
        $this->assign('all_page',$all_page);
        $page = $data_list->render('上一页','下一页');
        $this->assign('page',$page);
     
        $this->assign('data_list', $data_list);
        return $this->fetch('subbill');
    }

    /**
    *新建子账户
    */
    public function addsubbill(){
        if($this->request->isPost()){
            $data = $this->request->post();
            if(empty($data['subbillname']) || empty($data['name']) || empty($data['pwd']) || empty($data['roleid'])){
                $this->error('请完善信息');
            }
            $res = db('member')->where('subbillname',$data['subbillname'])->find();
            if($res){
                $this->error('该账户已存在');
            }
            $uid = session('uid');
            $addData['name']        = $data['name'];
            $addData['subbillname'] = $data['subbillname'];
            $addData['qq']          = $data['qq'];
            $addData['verify']      = genRandomString(6);
            $addData['password']    = MemberModel::encryption($data['pwd'], $addData['verify']);
            $addData['roleid']      = $data['roleid'];
            $addData['create_time'] = time();
            $addData['update_time'] = time();
            $addData['parentid']    = $uid;
            $result = db('member')->insert($addData);
            if($result){
                $this->success('添加成功',url('member/subbill'));
            }else{
                $this->error('添加失败');
            }
        }

        //角色列表
        $role_list = db('member_role')->where('status','1')->select();
        $this->assign('role_list', $role_list);

        return $this->fetch('addsubbill');
    }

    /**
    *修改子账户
    */
    public function editsubbill(){
        if($this->request->isPost()){
            $data = $this->request->post();
            if(empty($data['subbillname']) || empty($data['name']) || empty($data['roleid'])){
                $this->error('请完善信息');
            }
            $where['subbillname'] = $data['subbillname'];
            $where['id']          = array('neq', $data['id']);
            $res = db('member')->where($where)->find();
            if($res){
                $this->error('该账户已存在');
            }
            $uid = session('uid');
            $addData['name']        = $data['name'];
            $addData['subbillname'] = $data['subbillname'];
            $addData['qq']          = $data['qq'];
            if(!empty($data['pwd'])){
                $addData['verify']   = genRandomString(6);
                $addData['password'] = MemberModel::encryption($data['pwd'], $addData['verify']);
            }        
            $addData['roleid']      = $data['roleid'];
            $addData['update_time']  = time();
            $result = db('member')->where(['id' => $data['id'], 'parentid' => $uid])->update($addData);
            if($result){
                $this->success('修改成功',url('member/subbill'));
            }else{
                $this->error('修改失败');
            }
        }

        $id = input('id');
        $info = db('member')->where('id',$id)->find();
        $this->assign('info',$info);

        //角色列表
        $role_list = db('member_role')->where('status','1')->select();
        $this->assign('role_list', $role_list);

        return $this->fetch('editsubbill');
    }

    /**
    *删除子账户
    */
    public function del_subbill($id = 0){
        $id  = input('id');
        $uid = session('uid');
        $result = db('member')->where(['id' => $id, 'parentid' => $uid])->delete();
        if($result){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }

    /**
    *删除多条子账号
    */
    public function del_allsubbill($items = ''){
        $items  = input('items');
        if(empty($items)){
            $this->error('请选择需要删除的子账号');
            exit;
        }
        $items  = explode(',', $items);
        $uid    = session('info.memberid');
        $result = '';
        foreach ($items as $key => $value) {
            $result = db('member')->where(['id' => $value, 'parentid' => $uid])->delete();
        }
        if($result){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }

    //退出登录
    public function loginout(){
        if ($this->mid) {
            cookie('auto', null);
            session('uid', null);
            session('company', null);
            session('user_name', null);
            session('puid',null);
            session('mobile', null);
            $this->redirect('member/login/index');
        } else {
            $this->error('已经退出！',url('member/login/index'));
        }
    }


    
}